import App from "./App.vue";
import { createApp } from "vue";
/** 初始化全局共享数据 */
import { initGlobalStore } from "@/store";
/**
 * Element-Plus
 */
import ElementPlus from "element-plus";
// import zhCn from "element-plus/lib/locale/lang/zh-cn";
/** microApp */
import microApp from "@micro-zoe/micro-app";
import { createRouter, createWebHistory } from "vue-router";

microApp.start({
  /** 防止子应用请求父应用资源（部署时需要配置这个url指向这个文件） */
  iframeSrc: '/admin/empty.html',
  'keep-router-state': true,
  // "disable-memory-router": true, /** 关闭虚拟路由 */
  globalAssets: {
    js: [
      // 'https://tinymce.boshiyun.com.cn/tinymce2/tinymce.min.js',
      // 'https://cdn.bootcdn.net/ajax/libs/vue/3.2.41/vue.esm-browser.prod.min.js',
      // 'https://cdn.bootcdn.net/ajax/libs/vue-router/4.1.6/vue-router.esm-browser.min.js',
    ],
    css: [
      // 'https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.css',
    ],
  },
  // excludeAssetFilter (assetUrl) {
  //   if (assetUrl === 'ws://localhost:2666') {
  //     return true // 返回true则micro-app不会劫持处理当前文件
  //   }
  //   return false
  // }
});

const router = createRouter({
  history: createWebHistory('/admin/'),
  routes: [
    {
      path: "/:subAppName/:path*",
      name: "子应用",
      component: () => import("@/Content.vue"),
      meta: { hidden: true, title: "子应用" },
    },
  ],
});

router.beforeEach(async (to, from, next) => {
  console.log('目标路由:', to.fullPath);
  next();
})

/** 初始化Global数据 */
initGlobalStore();

let app: any = null;
(window as any).mount = () => {
  app = createApp(App);
  app
    .use(ElementPlus, {
      // locale: zhCn,
    })
    .use(router);
  app.mount("#app-microapp-main");
}


(window as any).unmount = () => {
  app?.unmount();
  app = null;
}

if (!(window as any).__MICRO_APP_ENVIRONMENT__) {
  (window as any).mount();
}
